﻿#region

using System;
using MediaSage.Lib;
using MediaSage.Lib.Configuration;
using MediaSage.Lib.Plugin;
using MediaSage.Lib.ThreadManager;

#endregion

namespace MediaSageService.ScheduledTasks
{
    ///<summary>
    ///    Class for scheduled tasks, includes running of said tasks, scheduling, etc
    ///</summary>
    public class Task : IComparable<Task>
    {
        #region Fields

        private readonly Logger logger;

        /// <summary>
        /// Used for maintenance providers
        /// </summary>
        private readonly object taskProvider;
        
        private Config config;
        private DateTime nextRunTime;
        private TimeSpan runInterval;
        private Guid taskID;
        private string taskName;
        private Status taskStatus;
        private ThreadManager threadManager;
        
        #endregion Fields

        #region Constructors

        public Task(Config Configuration, ThreadManager ThreadManager, PluginBase Provider, TimeSpan RunInterval,
                    DateTime? NextRun)
        {
            config = Configuration;
            logger = Configuration.Logger;
            taskStatus = Status.Undefined;
            threadManager = ThreadManager;
            if (NextRun == null)
            {
                NextRun = DateTime.Now;
            }
            nextRunTime = (DateTime) NextRun;
            runInterval = RunInterval;
            taskProvider = Provider;
            logger.Log("MediaSageService.ScheduledTasks.Task initialized");
        }
        
        #endregion Constructors

        #region Enumerations

        #region Status enum

        /// <summary>
        ///     Holds the status of this task
        /// </summary>
        public enum Status
        {
            Undefined,
            Running,
            Scheduled,
            Deleted,
            Error,
            Complete
        }

        #endregion Status enum

        #endregion Enumerations

        #region Methods

        /// <summary>
        ///     Allows us to sort by the next time to run
        /// </summary>
        /// <param name="other"> </param>
        /// <returns> 1,-1,0 </returns>
        public int CompareTo(Task other)
        {
            if (nextRunTime < other.nextRunTime)
            {
                return (1);
            }
            if (nextRunTime > other.nextRunTime)
            {
                return (-1);
            }

            return 0;
        }

        public DateTime GetNextRunTime()
        {
            return nextRunTime;
        }

        public object GetTask()
        {
            return taskProvider;
        }
        
        public Status GetStatus()
        {
            return taskStatus;
        }

        public string GetTaskName()
        {
            return taskName;
        }
        
        public void SetNextRunTime(DateTime value)
        {
            nextRunTime = value;
        }

        public void SetStatus(Status status)
        {
            taskStatus = status;
        }

        #endregion Methods
    }
}